package com.car.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.car.commons.core.model.Result;
import com.car.commons.core.model.WoniuException;
import com.car.commons.web.util.RedisKeyUtil;
import com.car.commons.web.util.RedisUtil;
import com.car.commons.web.util.StateUtil;
import com.car.order.client.CarShoppingClient;
import com.car.order.client.model.shopping.goodsInfo.GoodsInfoDto;
import com.car.order.dto.goods.GoodsDto;
import com.car.order.dto.goods.GoodsForShoppingCar;
import com.car.order.entity.ShoppingCar;
import com.car.order.entity.form.shoppingCar.ShoppingCarChangeForm;
import com.car.order.form.shoppingCar.ShoppingCarInsertForm;
import com.car.order.service.ShoppingCarService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 杜岷芫
 * @since 2024-09-05 18:59:29
 */
@Service
@Slf4j
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CarShoppingClient carShoppingClient;

    @Override
    public List<ShoppingCar> findList(Integer userId) throws Exception {
        Map<Object, Object> shoppingCar = redisUtil.hgetall(RedisKeyUtil.userShoppingCar(userId));
        Set<Map.Entry<Object, Object>> entries = shoppingCar.entrySet();
        ArrayList<ShoppingCar> shoppingCars = new ArrayList<>();
        for (Map.Entry<Object, Object> entry : entries) {
            shoppingCars.add(((ShoppingCar) entry.getValue()));
        }
        return shoppingCars;
    }

    @Override
    public ShoppingCar insert(ShoppingCarInsertForm form) throws Exception{
        ShoppingCar shoppingCar1 = BeanUtil.toBean(form, ShoppingCar.class);
        HashSet<Integer> set = new HashSet<>();
        Result<List<GoodsInfoDto>> goodsInfoListByIds = carShoppingClient.findGoodsInfoListByIds(set);
        if (goodsInfoListByIds.getCode() != 200) {
            throw new WoniuException(goodsInfoListByIds.getCode(), goodsInfoListByIds.getMsg());
        }
        GoodsInfoDto goodsInfoDto = goodsInfoListByIds.getData().get(0);
        if (ObjectUtil.isEmpty(goodsInfoDto)) {
            throw new WoniuException(500, "该商品不存在");
        }
        if (goodsInfoDto.getGoodsState().equals(StateUtil.GOODS_STATE_NOMAL)) {
            throw new WoniuException(500, "商品状态异常");
        }
        if (goodsInfoDto.getGoodsStock() < form.getGoodsCount()) {
            throw new WoniuException(500, "商品库存不足");
        }
        shoppingCar1.setGoods(goodsInfoDto);
        shoppingCar1.setGoodsState(goodsInfoDto.getGoodsState());
        shoppingCar1.setGoodsStock(goodsInfoDto.getGoodsStock());
        //判断购物车商品是否存在
        ShoppingCar shoppingCar = (ShoppingCar) redisUtil.hget(RedisKeyUtil.userShoppingCar(form.getUserId()), RedisKeyUtil.shoppingCarInfo(form.getUserId(), form.getGoodsId()));
        //判断不为空时候说明用户有购物车存在
        if(ObjectUtil.isNotEmpty(shoppingCar)){
            //更新数量
            shoppingCar1.setGoodsCount(shoppingCar1.getGoodsCount()+shoppingCar.getGoodsCount());
        }
        //更新redis数据
        redisUtil.hset(RedisKeyUtil.userShoppingCar(form.getUserId()),RedisKeyUtil.shoppingCarInfo(form.getUserId(), form.getGoodsId()),shoppingCar1);
        return shoppingCar;
    }

    @Override
    public void delete(Integer userId, Integer goodsId) throws Exception{
        Long hdel = redisUtil.hdel(RedisKeyUtil.userShoppingCar(userId), RedisKeyUtil.shoppingCarInfo(userId, goodsId));
        if (hdel == 0) {
            throw new WoniuException(500, "删除失败");
        }
    }

    @Override
    public ShoppingCar change(ShoppingCarChangeForm form) throws Exception{
        //获取对应的商品
        ShoppingCar shoppingCar = (ShoppingCar) redisUtil.hget(RedisKeyUtil.userShoppingCar(form.getUserId()), RedisKeyUtil.shoppingCarInfo(form.getUserId(), form.getGoodsId()));
        if (ObjectUtil.isEmpty(shoppingCar)) {
            throw new WoniuException(500, "购物车里没有该商品");
        }
        shoppingCar.setGoodsCount(form.getGoodsCount());
        redisUtil.hset(RedisKeyUtil.userShoppingCar(form.getUserId()),RedisKeyUtil.shoppingCarInfo(form.getUserId(), form.getGoodsId()),shoppingCar);
        return shoppingCar;
    }

}